import request from '@/utils/request'
import { API_CONFIG } from '@/api/config'

// 查询收货地址列表
export function listUserAddress(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/address/list',
    method: 'get',
    params: params
  })
}

// 查询收货地址详细
export function getUserAddress(addressId) {
  return request({
    url: '/mobile/address/' + addressId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 新增收货地址
export function addUserAddress(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/address',
    method: 'post',
    data: requestData
  })
}

// 修改收货地址
export function updateUserAddress(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/address',
    method: 'put',
    data: requestData
  })
}

// 删除用户地址
export function delUserAddress(addressId) {
  return request({
    url: '/mobile/address/' + addressId,
    method: 'delete',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 设置默认地址
export function setDefaultAddress(addressId) {
  return request({
    url: '/mobile/address/setDefault/' + addressId,
    method: 'put',
    data: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 获取默认地址
export function getDefaultAddress(userId) {
  return request({
    url: '/mobile/address/default',
    method: 'get',
    params: { 
      userId,
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 获取当前用户地址列表
export function getAddressByUserId(userId) {
  return request({
    url: '/mobile/address/list',
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
    // 不再需要传递userId参数，后端会从安全上下文获取
  })
}

// 批量删除用户地址
export function delUserAddressBatch(addressIds) {
  return request({
    url: '/mobile/address/batch/' + addressIds,
    method: 'delete',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 验证收货地址
export function validateAddress(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/address/validate',
    method: 'post',
    data: requestData
  })
}